package com.ocom.common.utils;

import lombok.extern.slf4j.Slf4j;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;


@Slf4j
public class DateUtil {

    public static ThreadLocal<DateFormat> chinaDateSDF = new ThreadLocal<DateFormat>() {

        @Override
        protected DateFormat initialValue() {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
            return df;

        }
    };

    /**
     * 获取日期yyyy-MM-dd格式字符串
     *
     * @param date
     * @param locale
     * @return
     */
    public static String dateToStr(Date date, Locale locale) {
        if (locale == null) {

        }
        return chinaDateSDF.get().format(date);
    }

    /**
     * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
     *
     * @param strDate
     * @return
     */
    public static Date strToDateLong(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }


    public static Long convertTimeToLong(String time) {
        DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime parse = LocalDateTime.parse(time, ftf);
        return LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    //获取当前时间
    public static String getDateTime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        return df.format(new Date());// new Date()为获取当前系统时间
    }

    //获取当前时间
    public static String getDate() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        return df.format(new Date());// new Date()为获取当前系统时间
    }

    //获取当前时间
    public static String getDate(Date date) {
        try {
            if(date==null){
                return CommonUtil.getCurrentDate();
            }
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
            return df.format(date);// new Date()为获取当前系统时间
        }catch (Exception e){
            log.error("转换异常【{}】",e);
        }
        return "2023-01-02";

    }

    //获取当前时间
    public static String getDateTime(Date date) {
        try {
            if(date==null){
                return CommonUtil.getCurrentDate();
            }
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            return df.format(date);// new Date()为获取当前系统时间
        }catch (Exception e){
            log.error("转换异常【{}】",e);
        }
        return "2023-01-02 03:04:05";

    }

    /*
     ** @author lu
     * @date 2019/9/16 13:13
     * @param [time 时间, timeFormat 时间格式]
     * @return java.lang.String
     * @description 时间转字符串
     */
    public static String getStrDate(Date time,String timeFormat) {
        SimpleDateFormat formatter = new SimpleDateFormat(timeFormat);
        String dateString = formatter.format(time);
        return dateString;
    }

    /*
     ** @author lu
     * @date 2019/9/16 13:13
     * @param [time 时间, timeFormat 时间格式]
     * @return java.lang.String
     * @description 时间转字符串
     */
    public static String getStrDate2(Date time,String timeFormat) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(timeFormat);
            String dateString = formatter.format(time);
            return dateString;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /*
     ** @author lu
     * @date 2019/9/16 13:10
     * @param [strDate, timeFormat] 时间的格式
     * @return java.vo.Date
     * @description 字符串到日期类型的转换
     */
    public static Date getDate(String strDate,String timeFormat) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat(timeFormat);
        Date currentTime_2 = formatter.parse(strDate);
        return currentTime_2;
    }

    /*
     ** @author lu
     * @date 2019/9/16 13:10
     * @param [strDate, timeFormat] 时间的格式
     * @return java.vo.Date
     * @description 字符串到日期类型的转换
     */
    public static Date getDate2(String strDate,String timeFormat) throws ParseException {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(timeFormat);
            Date currentTime_2 = formatter.parse(strDate);
            return currentTime_2;
        } catch (ParseException e) {
            return null;
        }
    }



    /**
     * 得到几天前的时间
     * @param d
     * @param day
     * @return
     */
    public static Date getDateBefore(Date d,int day){
        Calendar now =Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE,now.get(Calendar.DATE)-day);
        return now.getTime();
    }

    /**
     * 得到几天后的时间
     * @param d
     * @param day
     * @return
     */
    public static Date getDateAfter(Date d,int day){
        Calendar now =Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE,now.get(Calendar.DATE)+day);
        return now.getTime();
    }

    public static boolean timeStamp(long time) {
        int date=(int) (System.currentTimeMillis() / 1000);
        double minute = (double)(date-time)/ 60;
        if (-5<=minute && minute<=5){
            return true;
        }
        return false;
    }


    /**

     * 功能描述：返回小时

     *

     * @param date

     * 日期

     * @return 返回小时

     */
    public static int getHour(Date date) {

        Calendar calendar = Calendar.getInstance();

        calendar.setTime(date);

        return calendar.get(Calendar.HOUR_OF_DAY);

    }

    /**

     * 功能描述：返回分

     *

     * @param date

     * 日期

     * @return 返回分钟

     */

    public static int getMinute(Date date) {

        Calendar calendar = Calendar.getInstance();

        calendar.setTime(date);

        return calendar.get(Calendar.MINUTE);

    }

    /**
     * 在日期date上增加amount天 。
     *
     * @param date   处理的日期，非null
     * @param amount 要加的天数，可能为负数
     */
    public static Date addDays(Date date, int amount) {
        Calendar now =Calendar.getInstance();
        now.setTime(date);
        now.set(Calendar.DATE,now.get(Calendar.DATE)+amount);
        return now.getTime();
    }

    /**
     * 判断是否同一天
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isSameDay(Date date1, Date date2) {
        if (date1 == null || date2 == null) {
            return false;
        }

        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();

        cal1.setTime(date1);
        cal2.setTime(date2);

        // 比较年份、月份和日期部分
        return (cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
                cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH) &&
                cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH));
    }


    public static long countWorkingDays(Date date1, Date date2) {
        // 转换为java.time.LocalDate
        LocalDate startDate = date1.toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDate();
        LocalDate endDate = date2.toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDate();


        long workingDays = 1;
        while (startDate.isBefore(endDate)) {
            if (startDate.getDayOfWeek() != DayOfWeek.SATURDAY && startDate.getDayOfWeek() != DayOfWeek.SUNDAY) {
                workingDays++;
            }
            startDate = startDate.plusDays(1);
        }
        return workingDays;
    }

    public static void main(String[] args) {
        Date  dat1 =new Date();
        Date  dat2 = addDays(dat1,10);
        long workingDays = countWorkingDays(dat1, dat1);
        System.out.println("Working days between " + dat1 + " and " + dat2 + ": " + workingDays);
    }

}
